<?php

class Sistema_EnviodearquivocorrecaoController extends App_Controller_Action
{
    
    private $_table     = 'ENVIO_ARQUIVO_CORRECAO';
    private $_pk        = 'EVC_ID';
    private $_order_col = 'EVC_DATHOR';
    private $_order     = 'DESC';
    private $_sta_col   = 'EVC_STA';
    private $obj        = '';
    private $form       = '';
    public  $dir        = 'public/upload/enviodearquivocorrecao/';
    public  $_file_col  = 'EVC_FILE';
    
    private $_USU_ID;
    
    public function init()
    {
        $auth = Zend_Auth::getInstance();
        $s_user = $auth->getStorage();
        $this->_USU_ID = $s_user->read()->USU_ID;
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('elemform','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_enviodearquivo_list.js');
        
        $eva_id = $this->_getParam('eva_id', 0);
        $this->view->eva_id = $eva_id; 
        $where = ' EVA_ID = '.$eva_id;
       
        $dsfiltro = $this->_getParam('dsfiltro', ''); 
        if($dsfiltro != '') $where .= ' AND EVA_NOM LIKE "'. $dsfiltro.'%"';
        $this->view->dsfiltro = $dsfiltro;
        
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
        $query = 'SELECT SQL_CACHE  *, count(1) as TOTAL FROM ENVIO_ARQUIVO_CORRECAO '
                .' WHERE EVA_ID = '.$eva_id
                .' AND EVC_TPO = 1';
        $cor = $this->db->fetchRow($query);
        $this->view->cor = $cor;
        
        $cur = array();
        if($cor['ALU_ID'] > 0){
            $query = 'SELECT SQL_CACHE c.CUR_ID, c.CUR_REDENV, c.CUR_REDCOR, m.MAT_ID '
                .' FROM MATRICULA m, CURSO c '
                .' WHERE m.ALU_ID = '.$cor['ALU_ID'].' AND m.MAT_ID = '.$cor['MAT_ID']
                .' AND m.CUR_ID = c.CUR_ID '
                .' AND m.MAT_STA = 1';
            $cur = $this->db->fetchRow($query);
        }
        $this->view->cur = $cur;
        
    }
    
    public function editAction(){
        
        Zend_Loader::loadClass('imagem','library');
        Zend_Loader::loadClass('dominio','library');
        
        $eva_id = $this->_getParam('eva_id', 0);
        $this->view->eva_id = $eva_id; 
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){
                    $id = (int) $formData[$this->_pk];
                    
                    $data = $formData;
                    
                    $returnImg = imagem::upload($this->_file_col,$this->dir,$id);
                    
                    unset($data['submit']);
                    $data['EVC_NOM'] = $_FILES[$this->_file_col]['name'];
                    $data['EVC_DATHOR'] = date('Y-m-d H:i:s'); 
                    $data['EVC_TPO'] = 1;
                    $data['EVC_STA'] = 1;
                    $data['USU_ID'] = $this->_USU_ID;
                    
                    
                    $where = 'EVA_ID = '.$data['EVA_ID'];
                    $query = 'SELECT count(*) as TOTAL FROM '.$this->_table.' WHERE '.$where;
                    $row = $this->db->fetchRow($query);
                    $letra = dominio::getLetra($row['TOTAL'] + 1);
                    
                    $data['EVC_LTR'] = $letra;
                    $data['EVC_NOM'] = $eva_id.$letra;
                    
                    
                    $query = 'SELECT SQL_CACHE * FROM ENVIO_ARQUIVO WHERE EVA_ID = '.$eva_id;
                    $env = $this->db->fetchRow($query);
                    $data['ALU_ID'] = $env['ALU_ID'];
                    $data['MAT_ID'] = $env['MAT_ID'];
                    $data['CUR_ID'] = $env['CUR_ID'];
                    
                    if ($id == 0){
                        $this->db->insert($this->_table,$data);
                        //$id = $this->db->lastInsertId();
                        $id = $data['EVA_ID'];
                        imagem::rename($id.$letra,$this->dir,$returnImg['filenew']);
                        
                        $query = 'SELECT SQL_CACHE ALU_ID FROM ENVIO_ARQUIVO WHERE EVA_ID = '.$data['EVA_ID'];;
                        $eva = $this->db->fetchRow($query);
                        
                        $arr['ALU_ID'] = $eva['ALU_ID'];
                        Zend_Loader::loadClass('mensagem','library');
                        mensagem::correcaoredacao($arr);
                    }
                 
                    $urlOptions = array('controller'=>$this->_controller,'action'=>'index','eva_id'=>$eva_id);
                    $this->_helper->redirector->gotoRoute($urlOptions);
                } else {
                    $this->view->data = $formData;
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->view->data = $formData;
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id;
                $data = $this->db->fetchRow($query);
                $this->view->data = $data;
            }
        }
        
    }
    

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $data = array($this->_sta_col => 0);
            //$ok = (bool) $this->db->update($this->_table,$data, $this->_pk .' = ' . $id);
            $ok = (bool) $this->db->delete($this->_table,$this->_pk .' = ' . $id);
            
        }
        
        echo json_encode(array('ok' => $ok));
    }
    
}

